package com.sdy.dcsb.web.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.util.Map;

/**
 * @author Snaky丶
 * @date 2018/9/5 18:01
 * Function http脚本请求工具类
*/
public class Http4ScriptUtil {
    public String get(String url, Map<String,String> headMap){
        url = ChineseUtil.urlEncodeChinese(url);
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        try {
            setHeader(get,headMap);
            client.executeMethod(get);
            return IOUtils.toString(get.getResponseBodyAsStream(),"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            get.releaseConnection();
        }
        return "";
    }

    public String post4Multipart(String url, Map<String,String> bodyMap ,Map<String,String> headMap) throws Exception{
        url = ChineseUtil.urlEncodeChinese(url);
        HttpClient client = new HttpClient();

        PostMethod post = new PostMethod(url);
        post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        if(bodyMap != null){
            Part[] parts = new Part[bodyMap.size()];
            int i = 0;
            for (Map.Entry<String,String> entry:bodyMap.entrySet()) {
                parts[i] = new StringPart(entry.getKey(),entry.getValue(),"UTF-8");
                i++;
            }
            post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        }
        return reqWithHeader(client,post,headMap);
    }

    public String post4Json(String url,String jsonStr,Map<String,String> headMap) throws Exception{
        url = ChineseUtil.urlEncodeChinese(url);
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        post.setRequestEntity(new StringRequestEntity(jsonStr,"application/json","utf-8"));
        return reqWithHeader(client,post,headMap);
    }

    public String post4Json4Xml(String url,String jsonStr,Map<String,String> headMap) throws Exception{
        String result = post4Json(url,jsonStr,headMap);
        return JsonUtil.xml2JSON(result);
    }

    public String post4Urlencoded(String url,Map<String,String> bodyMap ,Map<String,String> headMap) throws Exception {
        url = ChineseUtil.urlEncodeChinese(url);
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.getParams().setContentCharset("UTF-8");
        post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        if(bodyMap != null){
            NameValuePair[] param = new NameValuePair[bodyMap.size()];
            int i = 0;
            for (Map.Entry<String,String> entry:bodyMap.entrySet()) {
                param[i] = new NameValuePair(entry.getKey(),entry.getValue());
                i++;
            }
            post.addParameters(param);
        }
        return reqWithHeader(client,post,headMap);
    }

    private void setHeader(HttpMethodBase method,Map<String,String> map){
        if(map != null){
            for (Map.Entry entry : map.entrySet()) {
                method.setRequestHeader(String.valueOf(entry.getKey()),String.valueOf(entry.getValue()));
            }
        }
    }

    private String reqWithHeader(HttpClient client, PostMethod post, Map<String,String> map) throws Exception{
        String response;
        try {
            setHeader(post,map);
            client.executeMethod(post);
            response = IOUtils.toString(post.getResponseBodyAsStream(),"UTF-8");
        } finally {
            post.releaseConnection();
        }
        return response;
    }

    public static void main(String[] args) {
        String result = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soap:Body><ns1:ExtQueryResponse xmlns:ns1=\"http://service.ws.rpe.buha.cn\"><ns1:out><ns1:ArrayOfString><ns1:string>000</ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string><ns1:string xsi:nil=\"true\"></ns1:string></ns1:ArrayOfString><ns1:ArrayOfString><ns1:string>HH</ns1:string><ns1:string>SFZH</ns1:string><ns1:string>XM</ns1:string><ns1:string>CYM</ns1:string><ns1:string>JTGX</ns1:string><ns1:string>MZ</ns1:string><ns1:string>XB</ns1:string><ns1:string>SSSSXQ</ns1:string><ns1:string>ZZXZ</ns1:string><ns1:string>LRRQ</ns1:string><ns1:string>SSGAJG</ns1:string><ns1:string>HLRRQ</ns1:string><ns1:string>BSC</ns1:string><ns1:string>JWH</ns1:string></ns1:ArrayOfString><ns1:ArrayOfString><ns1:string>18022958</ns1:string><ns1:string>410105197505101022</ns1:string><ns1:string>肖丽</ns1:string><ns1:string></ns1:string><ns1:string>38</ns1:string><ns1:string>01</ns1:string><ns1:string>2</ns1:string><ns1:string>410103</ns1:string><ns1:string>航海南路２号院５７号楼３５号</ns1:string><ns1:string>1980-01-01 00:00:00.0</ns1:string><ns1:string>郑密路派出所</ns1:string><ns1:string>2005-08-20 11:50:46.0</ns1:string><ns1:string>郑密路办事处</ns1:string><ns1:string>郑飞警务责任区</ns1:string></ns1:ArrayOfString><ns1:ArrayOfString><ns1:string>18022958</ns1:string><ns1:string>411223194409157531</ns1:string><ns1:string>陈向友</ns1:string><ns1:string></ns1:string><ns1:string>02</ns1:string><ns1:string>01</ns1:string><ns1:string>1</ns1:string><ns1:string>410103</ns1:string><ns1:string>航海南路２号院５７号楼３５号</ns1:string><ns1:string>1980-01-01 00:00:00.0</ns1:string><ns1:string>郑密路派出所</ns1:string><ns1:string>2005-08-20 11:50:46.0</ns1:string><ns1:string>郑密路办事处</ns1:string><ns1:string>郑飞警务责任区</ns1:string></ns1:ArrayOfString><ns1:ArrayOfString><ns1:string>18022958</ns1:string><ns1:string>411223195006087529</ns1:string><ns1:string>张彩英</ns1:string><ns1:string></ns1:string><ns1:string>12</ns1:string><ns1:string>01</ns1:string><ns1:string>2</ns1:string><ns1:string>410103</ns1:string><ns1:string>航海南路２号院５７号楼３５号</ns1:string><ns1:string>1980-01-01 00:00:00.0</ns1:string><ns1:string>郑密路派出所</ns1:string><ns1:string>2005-08-20 11:50:46.0</ns1:string><ns1:string>郑密路办事处</ns1:string><ns1:string>郑飞警务责任区</ns1:string></ns1:ArrayOfString><ns1:ArrayOfString><ns1:string>18022958</ns1:string><ns1:string>41122319740810751X</ns1:string><ns1:string>陈永民</ns1:string><ns1:string></ns1:string><ns1:string>20</ns1:string><ns1:string>01</ns1:string><ns1:string>1</ns1:string><ns1:string>410103</ns1:string><ns1:string>航海南路２号院５７号楼３５号</ns1:string><ns1:string>1980-01-01 00:00:00.0</ns1:string><ns1:string>郑密路派出所</ns1:string><ns1:string>2005-08-20 11:50:46.0</ns1:string><ns1:string>郑密路办事处</ns1:string><ns1:string>郑飞警务责任区</ns1:string></ns1:ArrayOfString><ns1:ArrayOfString><ns1:string>18022958</ns1:string><ns1:string>410103200410260065</ns1:string><ns1:string>陈可心</ns1:string><ns1:string></ns1:string><ns1:string>42</ns1:string><ns1:string>01</ns1:string><ns1:string>2</ns1:string><ns1:string>410103</ns1:string><ns1:string>航海南路２号院５７号楼３５号</ns1:string><ns1:string>2004-11-17 09:35:05.0</ns1:string><ns1:string>郑密路派出所</ns1:string><ns1:string>2005-08-20 11:50:46.0</ns1:string><ns1:string>郑密路办事处</ns1:string><ns1:string>郑飞警务责任区</ns1:string></ns1:ArrayOfString></ns1:out></ns1:ExtQueryResponse></soap:Body></soap:Envelope>";

        System.out.println(JsonUtil.xml2JSON(result));
    }
}